import { Controller } from 'egg';
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

export default class TagsController extends Controller {

  /**
   * @api {get} tags/list 获取标签列表
   *  
   * @apiName list
   * @apiGroup Tags
   * @apiParam {String} tagName 标签名
   * @apiDescription 获取标签列表 
   * 
   */
  public async list(ctx) {
    const { tagName } = ctx.request.query;
    let whereObject = tagName && tagName !== '' ? {
      tagName: {
        [Op.like]: `%${tagName}%`
      }
    } : {}
    const data = await ctx.model.Tag.findAndCountAll({
      where: whereObject
    });
    ctx.response.body = {
      code: 200,
      data: data.rows || [],
      total: data.count,
      status: '成功'
    };
  }

  /**
   * @api {post} tags/add 标签添加
   *  
   * @apiName add
   * @apiGroup Tags
   * @apiHeaderExample {json} headers:
   *     {
   *       "token": "admin_token"
   *     }
   * @apiParam {String} tagName 标签名
   * @apiDescription 标签添加
   *
   */
  public async add(ctx) {
    const { tagName } = ctx.request.body;
    try {
      let data = await ctx.model.Tag.findAll();

      data.forEach(item => {
        if (item.tagName === tagName) {
          throw ctx.helper.getErrorMsg(222);
        }
      });
      let mapArray = data.map(item => {
        return parseInt(item.tagId);
      });
      let maxValue = Math.max.apply(Math, mapArray.length === 0 ? [0] : mapArray);
      const newTagId = (maxValue + 1);
      await ctx.model.Tag.create({
        tagId: newTagId,
        tagName: tagName
      });
      ctx.response.body = {
        code: 200,
        status: '新增成功'
      };
    } catch (error_info) {
      ctx.response.body = error_info;
    }
  }

  /**
   * @api {post} tags/update 标签修改
   *  
   * @apiName update
   * @apiGroup Tags
   * @apiHeaderExample {json} headers:
   *     {
   *       "token": "admin_token"
   *     }
   * @apiParam {String} tagName 标签名
   * @apiParam {Integer} tagId 标签id
   * @apiDescription 标签修改
   *
   */
  public async update() {
    const { tagId, tagName } = this.ctx.request.body;
    const { whatType, getErrorMsg } = this.ctx.helper;
    try {
      if (whatType(tagId) === 'Undefined' || whatType(tagName) === 'Undefined') {
        throw getErrorMsg(207);
      }
      const tagList = await this.ctx.model.Tag.findAll();
      tagList.forEach(item => {
        if (item.tagName === tagName) {
          throw getErrorMsg(222);
        }
      });
      const tagIdArr = tagList.map(item => {
        return parseInt(item.tagId);
      }) || [];

      if (!tagIdArr.includes(parseInt(tagId))) {
        throw getErrorMsg(223);
      }
      this.ctx.model.Tag.update({
        tagName: tagName
      }, {
        where: {
          tagId: parseInt(tagId)
        }
      });
      this.ctx.response.body = {
        code: 200,
        status: '修改成功'
      }
    } catch (error_info) {
      this.ctx.response.body = error_info;
    }
  }

  /**
   * @api {get} tags/delete 标签删除
   *  
   * @apiName delete
   * @apiGroup Tags
   * @apiHeaderExample {json} headers:
   *     {
   *       "token": "admin_token"
   *     }
   * @apiParam {Integer} tagId 标签id
   * @apiDescription 标签删除
   *
   */
  public async delete(ctx) {
    const { tagId } = ctx.request.query;
    await ctx.model.Tag.destroy({
      where: {
        tagId: parseInt(tagId)
      }
    });
    ctx.response.body = {
      code: 200,
      status: '删除成功'
    }
  }
}
